package com.sskj.tibi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.enums.CodeTypeEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.SendSmsProvider;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CaptchaUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.WithdrawBalanceBean;
import com.sskj.tibi.component.DaggerUserDataComponent;
import com.sskj.tibi.presenter.WithdrawActivityPresenter;

import java.math.BigDecimal;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;


@Route(path = RConfig.TIBI_WITHDRAW)//提币
public class WithdrawActivity extends BaseActivity<WithdrawActivityPresenter> {

    @BindView(R2.id.tvAvailable)
    TextView tvAvailable;
    @BindView(R2.id.etAddress)
    EditText etAddress;
    @BindView(R2.id.ivAddress)
    ImageView ivAddress;
    @BindView(R2.id.etNum)
    EditText etNum;

    @BindView(R2.id.tvAll)
    TextView tvAll;

    @BindView(R2.id.tvFee)
    TextView tvFee;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCode)
    TextView tvGetCode;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BottomSheetDialog bottomSheetDialogCheck;
    @Autowired
    String money;
    private Disposable timeDispos;
    private BottomSheetDialog bottomSheetDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.tibi_activity_withdraw;
    }

    @Override
    public WithdrawActivityPresenter getPresenter() {
        return new WithdrawActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);

        setTitle("提币");
        setRightImg(R.mipmap.tibi_icon_record, v -> {
            ARouter.getInstance().build(RConfig.TIBI_WITHDRAW_RECORD).navigation();
        });
        ClickUtil.click(ivAddress, () -> {
            ARouter.getInstance().build(RConfig.TIBI_ADDRESS_LIST).withBoolean(Constans.IS_SELECT, true).navigation();
        });
        tvAvailable.setText(String.format("(可用%s)", money));
        LiveDataBus.get().with(RxBusCode.ADDRESS_POST, String.class)
                .observe(this, o -> addressBack(o));
    }


    @Override
    protected void initData() {
        mPresenter.getBalance();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    public void updateUI(WithdrawBalanceBean data) {
        tvAvailable.setText(String.format("(可用%s)", data.getUseFund()));
        etNum.setHint(String.format("最小提币数量%s", data.getTbMinRate()));
        tvFee.setText(String.format("%s%%", data.getTbFeeRate() + ""));
//        ClickUtil.click(tvAll, () -> {
//            etNum.setText(data.getUseFund());
//        });
        ClickUtil.click(tvGetCode, () -> {
            CaptchaUtil.check(this, (result, validate, msg) -> {
                SendSmsProvider sendSmsProvider = ARouter.getInstance().navigation(SendSmsProvider.class);
                sendSmsProvider.send(this, userData.getTel(), SPUtil.get(SPConfig.AREA_CODE, ""), CodeTypeEnum.STATUS_7, validate, new SendSmsProvider.OnSend() {
                    @Override
                    public void onSuccess() {
                        tvGetCode.setEnabled(false);
                        DisposUtil.close(timeDispos);
                        timeDispos = SendCodeUtil.timeCount(WithdrawActivity.this, tvGetCode);
                    }

                });
            });


        });
        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                mPresenter.withdraw(etAddress.getText().toString(), etNum.getText().toString(), etPwd.getText().toString(), etCode.getText().toString(), "USDT", null);
//                if (userData.getGoogleAuthenticationStatus()==1){
//                    bottomSheetDialog = BottomSheetUtil.showCheck(this, App.INSTANCE.getString(R.string.tibiwithdrawActivity4), false, true, false, null, (pwd, googleCode, smsCode) -> {
//                        bottomSheetDialog.dismiss();
//                        mPresenter.withdraw(etAddress.getText().toString(), etNum.getText().toString(), etPwd.getText().toString(), etCode.getText().toString(), data.getTbFeeRate(), googleCode);
//
//                    });
//                    bottomSheetDialog.show();
//                }else {
//                    mPresenter.withdraw(etAddress.getText().toString(), etNum.getText().toString(), etPwd.getText().toString(), etCode.getText().toString(), data.getTbFeeRate(),null);
//                }
            }

        });
        RxTextView.afterTextChangeEvents(etNum)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String s = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(s)) {
                        return;
                    }
                    BigDecimal subtract = new BigDecimal(s).subtract(new BigDecimal(data.getTbFeeRate()));
                    double max = Math.max(0, subtract.doubleValue());
                });
    }

    private boolean checkParam() {

        if (TextUtils.isEmpty(etAddress.getText())) {
            ToastUtil.showShort("请输入地址");
            return false;
        }
        if (TextUtils.isEmpty(etNum.getText())) {
            ToastUtil.showShort("请输入数量");
            return false;
        }
        if (TextUtils.isEmpty(etCode.getText())) {
            ToastUtil.showShort("请输入验证码");
            return false;
        }
        if (TextUtils.isEmpty(etPwd.getText())) {
            ToastUtil.showShort("请输入资金密码");
            return false;
        }
        return true;
    }

    public void addressBack(String addressPostBean) {
        if (etAddress != null) {
            etAddress.setText(addressPostBean);
        }
    }

    public void withdrawSuccess() {
        finish();
    }


}
